import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x00_aleret_item.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x01_alert_dialog_routes.dart';

class DatePickerPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    Future<DateTime> _showDatePicker1() {
      var date = DateTime.now();
      return showDatePicker(
        context: context,
        initialDate: date,
        firstDate: date,
        lastDate: date.add(
          Duration(
            days: 30
          )
        )
      );
    }


    Future<DateTime> _showDatePicker2({Function dateChanged, Function dateConfirmed}) {
      var date = DateTime.now();

      CupertinoDatePicker datePicker = CupertinoDatePicker(
        mode: CupertinoDatePickerMode.dateAndTime,
        minimumDate: date,
        maximumDate: date.add(
          Duration(
            days: 30 * 20
          )
        ),
        onDateTimeChanged: (value) {
          date = value;
          dateChanged(value);
        },
      );

      return showCupertinoModalPopup(
        context: context,
        builder: (ctx) {
          return Container(
            color: Colors.red[100],
            child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  SizedBox(
                    height: 44,
                    width: double.infinity,
                    child: Container(
                      color: Colors.red[200],
                      alignment: Alignment.centerRight,
                      child: FlatButton(
                        child: Text("Confirm"),
                        onPressed: (){
                          dateConfirmed(date);
                        },
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 200,
                    child: datePicker,
                  )
                ],
              ),
          );
        }
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: Text("Loading Page"),
      ),
      body: Padding(
        padding: EdgeInsets.only(
          top: 18,
          bottom: 18
        ),
        child: ListView(
          children: <Widget>[
            AlertItem("Material Date Picker", () async {
              var date = await _showDatePicker1();
              alert(context, content: "date : $date");
            }),

            AlertItem("Cupertino Date Picker", () async {
              var date = await _showDatePicker2(
                dateChanged: (value) => print("value changed : $value"),
                dateConfirmed: (value) => alert(context, content: "date : $value"),
              );
            }),
          ],
        ),
      ),
    );
  }
}